package com.crm.common.util;

import cn.hutool.core.date.DateUtil;
import com.beust.jcommander.internal.Lists;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description:
 * @author: lixq-f
 * @create: 2021/04/16
 */
public class DateUtils extends DateUtil {


    public static String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static String DATE_FORMAT_YYYY_MM = "yyyy-MM";

    public static String DATE_FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    public static String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";



    /**
     * 获取当前年
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        int weekYear = calendar.get(Calendar.YEAR);//获得当前的年
        return weekYear;
    }

    /**
     * 获取今天
     *
     * @return String
     */
    public static String getToday() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    /**
     * 获取昨天
     *
     * @return String
     */
    public static String getYestoday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    /**
     * 获取本月开始日期
     *
     * @return String
     **/
    public static String getMonthStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }

    /**
     * 获取某年某月开始日期
     *
     * @return String
     **/
    public static String getMonthStart(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }

    /**
     * 获取本月最后一天
     *
     * @return String
     **/
    public static String getMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
    }

    /**
     * 获取某年某月最后一天
     *
     * @return String
     **/
    public static String getMonthEnd(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
    }

    /**
     * 获取本周的第一天
     *
     * @return String
     **/
    public static String getWeekStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }

    /**
     * 获取本周的最后一天
     *
     * @return String
     **/
    public static String getWeekEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
    }

    /**
     * 获取本年的第一天
     *
     * @return String
     **/
    public static String getYearStart() {
        return new SimpleDateFormat("yyyy").format(new Date()) + "-01-01 00:00:00";
    }

    /**
     * 获取某年的第一天
     *
     * @return String
     **/
    public static String getYearStart(int year) {
        return year + "-01-01 00:00:00";
    }

    /**
     * 获取本年的最后一天
     *
     * @return String
     **/
    public static String getYearEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast) + " 23:59:59";
    }

    /**
     * 获取某年的最后一天
     *
     * @return String
     **/
    public static String getYearEnd(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast) + " 23:59:59";
    }

    /**
     * 获取某年某周的时间跨度
     *
     * @param year
     * @param week
     * @return
     */
    public static Map<String, String> getWeekRangeMap(int year, int week) {
        Map<String, String> timeMap = new HashMap<String, String>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        int weekYear = calendar.get(Calendar.YEAR);//获得当前的年
        calendar.setWeekDate(weekYear, week, 2);//获得指定年的第几周的开始日期
        Date time = calendar.getTime();
        String startTime = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
        timeMap.put("startTime", startTime);
        calendar.setWeekDate(weekYear, week, 1);//获得指定年的第几周的结束日期
        time = calendar.getTime();
        String endTime = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
        timeMap.put("endTime", endTime);
        return timeMap;
    }

    /**
     * 获取某年有多少周
     *
     * @param year
     * @return
     * @throws
     */
    public static int getYearWeekConut(int year) {
        int week = 52;
        try {
            /*Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
            calendar.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
            week = calendar.get(Calendar.WEEK_OF_YEAR);*/
            Map<String, String> timeMap = getWeekRangeMap(year, 53);
            if (!CollectionUtils.isEmpty(timeMap)) {
                String startTime = timeMap.get("startTime");
                if (startTime.substring(0, 4).equals(year + "")) { //判断年度是否相符，如果相符说明有53个周。
                    week = 53;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return week;
    }

    /**
     * 获取某年所有周的时间跨度
     *
     * @param year
     * @return
     */
    public static Map<String, Map<String, String>> getYearWeekMap(int year) {
        int weeks = getYearWeekConut(year);
        Map<String, Map<String, String>> yearWeekMap = new LinkedHashMap<>();
        for (int i = 1; i <= weeks; i++) {
            Map<String, String> timeMap = getWeekRangeMap(year, i);
            yearWeekMap.put(i + "", timeMap);
        }
        return yearWeekMap;
    }


    /**
     * 获取开始与结束时间之间的日期列表
     * @param startTime
     * @param endTime
     * @param dateFormat
     * @return
     */
    public static List<String> getBetweenDateList(Date startTime, Date endTime, String dateFormat, int offset){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        List<String> dateList = Lists.newArrayList();
        while(startTime.compareTo(endTime) != 1){
            dateList.add(sdf.format(startTime));
            startTime.setTime(startTime.getTime() + 1000 * 60 * 60 * 24 * offset);
        }
        return dateList;
    }

     /***
      * @Description: 判断日期是否超过90天
      * @Param: dateStartTime 合同开始时间
      * @Param: dataEndTime 合同结束时间
      * @Author: luojie
      * @Date: 2021/4/20 15:38
      */
     public static boolean isOutOfDate(Date nowDate,Date dataEndTime) throws ParseException {
           long margin = 0;

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            // 现在时间
            nowDate = sdf.parse(sdf.format(nowDate));

            // 出入时间
            dataEndTime = sdf.parse(sdf.format(dataEndTime));

            margin = nowDate.getTime() - dataEndTime.getTime();

            margin = margin / (1000 * 60 * 60 * 24);

            if (margin <= 90) {
                return false;
            }

            return true;
    }

}
